﻿// posts.cpp: 实现文件
//

#include "pch.h"
#include "luntan.h"
#include "posts.h"
#include "afxdialogex.h"
#include"User.h"
#include"luntanDlg.h"

// posts 对话框

IMPLEMENT_DYNAMIC(posts, CDialogEx)

posts::posts(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_USER2, pParent)
	, m_post_title(_T(""))
	, m_post_content(_T(""))
	, username1(_T(""))
{

}

posts::~posts()
{
}

void posts::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO1, m_boardname);
	DDX_Text(pDX, IDC_EDIT1, m_post_title);
	DDX_Text(pDX, IDC_EDIT2, m_post_content);
	DDX_Text(pDX, IDC_EDIT3, username1);
	DDX_Control(pDX, IDC_LIST1, m_list1);
}


BEGIN_MESSAGE_MAP(posts, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON8, &posts::OnBnClickedButton8)
	ON_BN_CLICKED(IDC_BUTTON5, &posts::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON1, &posts::OnBnClickedButton1)
	ON_CBN_SELCHANGE(IDC_COMBO1, &posts::OnCbnSelchangeCombo1)
	ON_BN_CLICKED(IDC_BUTTON6, &posts::OnBnClickedButton6)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, &posts::OnLvnItemchangedList1)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, &posts::OnNMClickList1)
	ON_BN_CLICKED(IDC_BUTTON3, &posts::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON9, &posts::OnBnClickedButton9)
	ON_BN_CLICKED(IDC_BUTTON10, &posts::OnBnClickedButton10)
	ON_BN_CLICKED(IDC_BUTTON11, &posts::OnBnClickedButton11)
	ON_BN_CLICKED(IDC_BUTTON7, &posts::OnBnClickedButton7)
END_MESSAGE_MAP()


// posts 消息处理程序


void posts::OnBnClickedButton8()
{
	// TODO: 在此添加控件通知处理程序代码
	posts::OnOK();
	CluntanDlg a;
	a.DoModal();
}

void posts::OnBnClickedButton5()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_post_title != "" && m_post_content != "" && username1 != "")
	{
		if (_boardname == "计算机")
			board_id = "0001";
		else if (_boardname == "Java")
			board_id = "0002";
		else if (_boardname == "C++")
			board_id = "0003";
		else if (_boardname == "windows程序设计")
			board_id = "0004";
		else if (_boardname == "数据库")
			board_id = "0005";
		else if (_boardname == "人工智能")
			board_id = "0006";
		else if (_boardname == "大数据")
			board_id = "0007";
		ADOConn m_AdoConn;
		m_AdoConn.OnInitADOConn();
		CString sql1, sql2;
		sql1 = "select count(Post_id) from Posts";
		m_AdoConn.m_pRecordset = m_AdoConn.GetRecordSet((_bstr_t)sql1);
		post_id = m_AdoConn.m_pRecordset->GetCollect("");
		post_id.Format("%05d", atoi(post_id) + 1);

		sql2.Format("insert into Posts(Post_id,Board_id,User_Name,Post_title,Post_content) values('%s','%s','%s','%s','%s')", \
			post_id, board_id, username1, m_post_title, m_post_content);
		m_AdoConn.ExecuteSQL((_bstr_t)sql2);
		//MessageBox("发布成功","发布结果");
		m_list1.DeleteAllItems();
		m_post_title = "", m_post_content = "", username1 = "";
		UpdateData(FALSE);
		m_list1.DeleteAllItems();
		AddToGrid(); //向列表视图控件中插入数据
		m_post_id = "";
	}
	else
	{
		MessageBox("帖子信息不能为空！", "提示", MB_ICONWARNING);
	}

}


BOOL posts::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化

	m_list1.SetExtendedStyle(LVS_EX_FLATSB //扁平风格显示滚动条
		| LVS_EX_FULLROWSELECT //允许整行选中
		| LVS_EX_HEADERDRAGDROP //允许整列拖动
		| LVS_EX_ONECLICKACTIVATE //单击选中项
		| LVS_EX_GRIDLINES); //画出网格线

	//设置表头
	m_list1.InsertColumn(1, _T("帖子标题"), LVCFMT_LEFT, 100, 25);
	m_list1.InsertColumn(2, _T("帖子内容"), LVCFMT_LEFT, 200, 25);
	m_list1.InsertColumn(3, _T("发帖者"), LVCFMT_LEFT, 70, 25);
	m_list1.InsertColumn(4, _T("所属版块"), LVCFMT_LEFT, 90, 25);
	m_list1.InsertColumn(5, _T("发帖时间"), LVCFMT_LEFT, 100, 25);
	m_list1.InsertColumn(0, _T("帖子ID"), LVCFMT_LEFT, 50, 25);


	AddToGrid();

	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	CString sql, boardname;
	sql = "select * from Board";
	m_AdoConn.m_pRecordset = m_AdoConn.GetRecordSet((_bstr_t)sql);
	while (!m_AdoConn.m_pRecordset->adoEOF)
	{
		m_boardname.AddString((char*)(_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Board_name"));
		m_AdoConn.m_pRecordset->MoveNext();
	}
	m_boardname.SetCurSel(0);
	m_boardname.GetLBText(m_boardname.GetCurSel(), _boardname);

	m_AdoConn.ExitConnect();
	UpdateData(false);
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void posts::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	m_list1.DeleteAllItems();
	AddToGrid(); //向列表视图控件中插入数据
	
}


void posts::OnCbnSelchangeCombo1()
{
	// TODO: 在此添加控件通知处理程序代码
	m_boardname.GetLBText(m_boardname.GetCurSel(),_boardname);
}


void posts::AddToGrid()
{
	// TODO: 在此处添加实现代码.
	ADOConn m_AdoConn; //ADOConn对象
	m_AdoConn.OnInitADOConn(); //连接数据库
	CString sql;
	int i = 0;
	sql.Format("select Post_id,Post_title,Post_content,Posts.User_Name,Board_name,Post_createtime from Posts inner join Board on Posts.Board_id=Board.Board_id");
	m_AdoConn.m_pRecordset = m_AdoConn.GetRecordSet((_bstr_t)sql); //根据SQL语句进行查询
	//MessageBox((_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_Name"));
	while (!m_AdoConn.m_pRecordset->adoEOF)
	{
		m_list1.InsertItem(i, ""); //向列表视图控件中插入行
		//向列表视图控件中插入列
		m_list1.SetItemText(i, 0, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_id"));
		m_list1.SetItemText(i, 1, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_title"));
		m_list1.SetItemText(i, 2, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_content"));
		m_list1.SetItemText(i, 3, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_Name"));
		m_list1.SetItemText(i, 4, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Board_name"));
		m_list1.SetItemText(i, 5, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_createtime")); 
		m_AdoConn.m_pRecordset->MoveNext(); //将记录集指针移动到下一条记录
		i++;
	}
	m_AdoConn.ExitConnect();
}


void posts::OnBnClickedButton6()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_post_id != "")
	{
		ADOConn m_AdoConn; //ADOConn对象
		m_AdoConn.OnInitADOConn(); //连接数据库
		CString sql;
		sql.Format("delete from Posts where Post_id='%s'", m_post_id);
		m_AdoConn.ExecuteSQL((_bstr_t)sql);
		m_AdoConn.ExitConnect();
		m_list1.DeleteAllItems();
		AddToGrid(); //向列表视图控件中插入数据
		m_post_id = "";
	}
	else
	{
		MessageBox("请选择要删除的帖子！", "提示", MB_ICONWARNING);
	}
}


void posts::OnLvnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}


void posts::OnNMClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	int pos = m_list1.GetSelectionMark();
	m_post_id=m_list1.GetItemText(pos, 0);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}




void posts::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_post_title != "")
	{
		m_list1.DeleteAllItems();
		ADOConn m_AdoConn;
		m_AdoConn.OnInitADOConn();
		CString sql;
		int i = 0;
		sql.Format("select Post_id,Post_title,Post_content,Board_name,Posts.User_Name,Post_createtime from Posts inner join Board on Posts.Board_id = Board.Board_id where Post_title like '%s%s%s'", \
			"%", m_post_title, "%"); //设置SQL语句
		m_AdoConn.m_pRecordset = m_AdoConn.GetRecordSet((_bstr_t)sql); //根据SQL语句进行查询
		while (!m_AdoConn.m_pRecordset->adoEOF)
		{
			m_list1.InsertItem(i, ""); //向列表视图控件中插入行
			//向列表视图控件中插入列
			m_list1.SetItemText(i, 0, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_id"));
			m_list1.SetItemText(i, 1, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_title"));
			m_list1.SetItemText(i, 2, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_content"));
			m_list1.SetItemText(i, 3, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_Name"));
			m_list1.SetItemText(i, 4, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Board_name"));
			m_list1.SetItemText(i, 5, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_createtime"));
			m_AdoConn.m_pRecordset->MoveNext(); //将记录集指针移动到下一条记录
			i++;
		}
		m_AdoConn.ExitConnect();
		m_post_title = "";
		UpdateData(false);
	}
	else
	{
		MessageBox("查询条件不能为空！", "提示", MB_ICONWARNING);
	}
}


void posts::OnBnClickedButton9()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_post_content != "")
	{
	m_list1.DeleteAllItems();
	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	CString sql;
	int i = 0;
	sql.Format(_T("select Post_id,Post_title,Post_content,Board_name,Posts.User_Name,Post_createtime from Posts inner join Board on Posts.Board_id = Board.Board_id where Post_content like '%s%s%s'"), "%", m_post_content, "%"); //设置SQL语句
	m_AdoConn.m_pRecordset = m_AdoConn.GetRecordSet((_bstr_t)sql); //根据SQL语句进行查询
	while (!m_AdoConn.m_pRecordset->adoEOF)
	{
		m_list1.InsertItem(i, ""); //向列表视图控件中插入行
		//向列表视图控件中插入列
		m_list1.SetItemText(i, 0, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_id"));
		m_list1.SetItemText(i, 1, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_title"));
		m_list1.SetItemText(i, 2, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_content"));
		m_list1.SetItemText(i, 3, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_Name"));
		m_list1.SetItemText(i, 4, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Board_name"));
		m_list1.SetItemText(i, 5, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_createtime"));
		m_AdoConn.m_pRecordset->MoveNext(); //将记录集指针移动到下一条记录
		i++;
	}
	m_AdoConn.ExitConnect();
	m_post_content = "";
	UpdateData(false);
	}
	else
	{
		MessageBox("查询条件不能为空！", "提示", MB_ICONWARNING);
	}
}


void posts::OnBnClickedButton10()
{
	// TODO: 在此添加控件通知处理程序代码
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	m_list1.DeleteAllItems();
	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	CString sql;
	int i = 0;
	sql.Format("select Post_id,Post_title,Post_content,Board_name,Posts.User_Name,Post_createtime from Posts inner join Board on Posts.Board_id = Board.Board_id where Board_name like '%s%s%s'", "%", _boardname, "%"); //设置SQL语句
	m_AdoConn.m_pRecordset = m_AdoConn.GetRecordSet((_bstr_t)sql); //根据SQL语句进行查询
	while (!m_AdoConn.m_pRecordset->adoEOF)
	{
		m_list1.InsertItem(i, ""); //向列表视图控件中插入行
		//向列表视图控件中插入列
		m_list1.SetItemText(i, 0, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_id"));
		m_list1.SetItemText(i, 1, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_title"));
		m_list1.SetItemText(i, 2, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_content"));
		m_list1.SetItemText(i, 3, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_Name"));
		m_list1.SetItemText(i, 4, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Board_name"));
		m_list1.SetItemText(i, 5, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_createtime"));
		m_AdoConn.m_pRecordset->MoveNext(); //将记录集指针移动到下一条记录
		i++;
	}

	m_AdoConn.ExitConnect();
}


void posts::OnBnClickedButton11()
{
	// TODO: 在此添加控件通知处理程序代码
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (username1 != "")
	{
	m_list1.DeleteAllItems();
	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	CString sql;
	int i = 0;
	sql.Format("select Post_id,Post_title,Post_content,Board_name,Posts.User_Name,Post_createtime from Posts inner join Board on Posts.Board_id = Board.Board_id where Posts.User_Name like '%s%s%s'","%", username1, "%"); //设置SQL语句
	m_AdoConn.m_pRecordset = m_AdoConn.GetRecordSet((_bstr_t)sql); //根据SQL语句进行查询
	while (!m_AdoConn.m_pRecordset->adoEOF)
	{
		m_list1.InsertItem(i, ""); //向列表视图控件中插入行
		//向列表视图控件中插入列
		m_list1.SetItemText(i, 0, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_id"));
		m_list1.SetItemText(i, 1, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_title"));
		m_list1.SetItemText(i, 2, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_content"));
		m_list1.SetItemText(i, 3, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_Name"));
		m_list1.SetItemText(i, 4, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Board_name"));
		m_list1.SetItemText(i, 5, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Post_createtime"));
		m_AdoConn.m_pRecordset->MoveNext(); //将记录集指针移动到下一条记录
		i++;
	}
	m_AdoConn.ExitConnect();
	username1 = "";
	UpdateData(false);
	}
	else
	{
		MessageBox("查询条件不能为空！", "提示", MB_ICONWARNING);
	}
}


void posts::OnBnClickedButton7()
{
	// TODO: 在此添加控件通知处理程序代码
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_post_title != "" && m_post_content != "" && username1 != "")
	{
		if (m_post_id != "")
		{
			ADOConn m_AdoConn; //ADOConn对象
			m_AdoConn.OnInitADOConn(); //连接数据库
			CString sql;
			sql.Format("update Posts set Post_title='%s',Post_content='%s',User_Name='%s' where Post_id='%s'", m_post_title, m_post_content, username1, m_post_id);
			m_AdoConn.ExecuteSQL((_bstr_t)sql);
			m_AdoConn.ExitConnect();
			m_post_title = "", m_post_content = "", username1 = "";
			UpdateData(FALSE);
			m_list1.DeleteAllItems();
			AddToGrid(); //向列表视图控件中插入数据
			post_id != "";
		}
		else
		{
			MessageBox("请选择要修改的帖子！", "提示", MB_ICONWARNING);

		}
	}
	else
	{
		MessageBox("修改的帖子信息不能为空！", "提示", MB_ICONWARNING);
	}

}
